//
//  UIImage+lvfjHelper.m
//  lvfj
//
//  Created by 吕飞俊 on 15/11/16.
//  Copyright © 2015年 Goudu KuaiEx Technology(Shenzhen)Co.,ltd. All rights reserved.
//   _          ______   _      _
//  | |        |  ____| (_)    | |
//  | |  __   _| |__ ___ _     | |_   _ _ __
//  | |  \ \ / /  __/ _ \ |_   | | | | | '_ \
//  | |___\ V /| | |  __/ | |__| | |_| | | | |
//  |______\_/ |_|  \___|_|\____/ \__,_|_| |_|
//

#define     kScreenHeight    [UIScreen mainScreen].bounds.size.height
#define     kScreenWidth     [UIScreen mainScreen].bounds.size.width
#define     kAppStateHeight     20

#import "UIImage+lvfjHelper.h"

@implementation UIImage (lvfjHelper)

#pragma mark getRealImage

-(instancetype)getRealImage{
    return [UIImage ImageRealWithImage:self];
}

+(instancetype)ImageRealWithStrimg:(NSString *)string{
    return [UIImage ImageRealWithImage:[UIImage imageNamed:string]];
}
+(instancetype)ImageRealWithImage:(UIImage *)image{
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

#pragma mark

-(NSUInteger)getImageData{
    NSData *data =  UIImagePNGRepresentation(self);
    return data.length;
}

+ (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation
{
    long double rotate = 0.0;
    CGRect rect;
    float translateX = 0;
    float translateY = 0;
    float scaleX = 1.0;
    float scaleY = 1.0;
    
    switch (orientation) {
        case UIImageOrientationLeft:
            rotate = M_PI_2;
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            translateX = 0;
            translateY = -rect.size.width;
            scaleY = rect.size.width/rect.size.height;
            scaleX = rect.size.height/rect.size.width;
            break;
        case UIImageOrientationRight:
            rotate = 33 * M_PI_2;
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            translateX = -rect.size.height;
            translateY = 0;
            scaleY = rect.size.width/rect.size.height;
            scaleX = rect.size.height/rect.size.width;
            break;
        case UIImageOrientationDown:
            rotate = M_PI;
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            translateX = -rect.size.width;
            translateY = -rect.size.height;
            break;
        default:
            rotate = 0.0;
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            translateX = 0;
            translateY = 0;
            break;
    }
    
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //做CTM变换
    CGContextTranslateCTM(context, 0.0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextRotateCTM(context, rotate);
    CGContextTranslateCTM(context, translateX, translateY);
    
    CGContextScaleCTM(context, scaleX, scaleY);
    //绘制图片
    CGContextDrawImage(context, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage);
    
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
    
    return newPic;
}

#pragma mark base64
+(UIImage *)imageWithDase64String:(NSString *)base64Str{
    NSData *imageData = [[NSData alloc] initWithBase64EncodedString:base64Str options:NSDataBase64DecodingIgnoreUnknownCharacters];
    return [UIImage imageWithData:imageData];
}
//base64字符串有图片前缀（前缀类似：）
+(UIImage *)imageWithHeaderDase64String:(NSString *)base64Str{
    NSURL *baseImageUrl = [NSURL URLWithString:base64Str];
    NSData *imageData = [NSData dataWithContentsOfURL:baseImageUrl];
    return[UIImage imageWithData:imageData];
}

#pragma mark 截屏

////1.等比缩放
//- (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize {
//    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
//    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
//    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    return scaledImage;
//}
//
////2.自定义大小
+(UIImage *)makeSizeImage:(UIImage *)image  size:(CGSize)reSize {
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
    [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return reSizeImage;
}
//
///等比例缩放.size
+(UIImage *)makeSizeImage:(UIImage *)image size:(CGFloat)size isWidth:(BOOL)isWidth{
    CGFloat scale = size/(isWidth?image.size.width:image.size.height);
    CGSize reSize = isWidth?CGSizeMake(size, image.size.height*scale):CGSizeMake(image.size.width*scale, size);
    
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
    [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return reSizeImage;
}
////3.处理某个特定的View
+(UIImage*)viewChangeImage:(UIView *)theView{
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

//+(UIImage *)innerImage:(UIImage *)image{
//    CGSize size = image.size;
//    CGImageRef ref = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, 0, size.width, size.height));//这里的rect切割是按图片的像素切割
//    UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), NO, 0);//最终获取得到的画布大小
//    CGContextRef con = UIGraphicsGetCurrentContext();
//    CGContextDrawImage(con, CGRectMake(0, 0, size.width, size.height), ref);
//
//    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    return newImage;
//}

//
////4.储存图片（存入到文件夹中）
//-(void)saveToPath:(UIImage *)img{
//    NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];
//    [img.PNGRepresentation writeToFile:path atomically:YES];
//}
//
////5.储存到手机的图片库中
//-(void)saveToimgLibrary{
//    CGImageRef screen = UIGetScreenImage();
//    UIImage* image = [UIImage imageWithCGImage:screen];
//    CGImageRelease(screen);
//    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
//}

////6.根据给定得图片，从其指定区域截取一张新得图片
+(UIImage *)cutImageFromImg:(UIImage *)img rect:(CGRect)rect{
    CGImageRef imageRef = img.CGImage;
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, rect);
    
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, rect, subImageRef);
    
    UIImage *resultImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
    return resultImage;
}

////7. 合并两张图片
+(UIImage *)appendingImage:(UIImage *)image1 toImage:(UIImage *)image2 {
    CGSize size1 = image1.size;
    CGSize size = CGSizeMake(MAX(size1.width, image2.size.width), size1.height+image2.size.height);
    UIGraphicsBeginImageContext(size);
    // Draw image1
    [image1 drawInRect:CGRectMake(0, 0, size.width, size1.height)];
    // Draw image2
    [image2 drawInRect:CGRectMake(0, size1.height, image2.size.width, image2.size.height)];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}

////8.截屏
+(UIImage *)ScreenShot:(UIView *)view rect:(CGRect)rect{
    CGFloat scale = [UIScreen mainScreen].scale;
    CGSize size = CGSizeMake(rect.size.width, rect.size.height);
    UIGraphicsBeginImageContextWithOptions(size, NO,scale);//设置截屏大小
    
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGImageRef imageRef =viewImage.CGImage;
    CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *sendImage =[[UIImage alloc] initWithCGImage:imageRefRect];
    CGImageRelease(imageRefRect);
    return sendImage;
}

+(UIImage *)ScreenShot:(UIView *)view h:(CGFloat)h{
    CGFloat scale = [UIScreen mainScreen].scale;
    CGSize size = CGSizeMake(kScreenWidth*scale, kScreenHeight*scale);
    UIGraphicsBeginImageContextWithOptions(size, NO,scale);//设置截屏大小
    
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGImageRef imageRef =viewImage.CGImage;
    CGRect rect = CGRectMake(0,kAppStateHeight, size.width, size.height);//这里可以设置想要截图的区域
    
    CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *sendImage =[[UIImage alloc] initWithCGImage:imageRefRect];
    CGImageRelease(imageRefRect);
    return sendImage;
}

@end
